﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using VK.Comm;
using VK.Casemeet.Areas;

namespace VK.Casemeet.Web.Admin
{
    public partial class AreaEdit : System.Web.UI.Page
    {
        public int areaID = 0;
        public string brandHtmlL = string.Empty, brandHtmlR = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            (this.Master as Site).SiteMapPathControl.MapPathList = new List<MapPath>(){
                new MapPath{ Text="桌面",Url="../Home.aspx"},
                new MapPath{ Text="地区",Url="/Admin/Merchants/AreaList.aspx"},
                new MapPath{ Text="编辑地区",Url="#"}
            };

            areaID = Util.GetQueryInt("AreaID");

            if (!IsPostBack)
            {
                LoadData();
            }
        }

        internal void LoadData()
        {
            if (areaID != 0)
            {
                AreaEntity area = AreaBLL.GetArea(areaID);
                AreaHelper.BindAreaDDL(ddlParentID, OptionType.Select, area.AreaID);
                txtName.Text = area.Name;
                if (area.ParentID == 0)
                {
                    trParentID.Visible = false;
                }
                else
                {
                    ddlParentID.SelectedValue = area.ParentID.ToString();
                }
            }
        }

        protected void butSave_Click(object sender, EventArgs e)
        {
            AreaEntity area = LoadEntity();
            area.AreaID = areaID;

            if (AreaBLL.UpdateArea(area) > 0)
            {
                Message.Msg(this.Page, CS.MsgEditSuccess, MessageType.Success);
            }
            else
            {
                Message.Msg(this.Page, CS.MsgEditFailure, MessageType.Failure);
            }
            LoadData();
        }

        protected AreaEntity LoadEntity()
        {
            AreaEntity area = AreaBLL.GetArea(areaID);
            area.OldParentID = area.ParentID;
            area.Name = txtName.Text.Trim();
            area.ParentID = int.Parse(ddlParentID.SelectedValue);
            
            return area;
        }
    }
}